package net.yambal.RTM.Method
{
	import flash.events.EventDispatcher;
	
	import net.yambal.RTM.Event.NoteEvent;
	import net.yambal.RTM.Event.RestErrorEvent;
	import net.yambal.RTM.Event.RestEvent;
	import net.yambal.RTM.Event.TimelineEvent;
	import net.yambal.RTM.Items.RTMTaskSeries;
	import net.yambal.RTM.Items.RTMTransaction;
	import net.yambal.RTM.RestWrapper.RestManager;
	import net.yambal.RTM.RestWrapper.RestService;
	import net.yambal.RTM.Utilty.Parser;
	import net.yambal.RTM.Utilty.RTMBooleanUtil;
	import net.yambal.RTM.Utilty.RTMTimelineGetter;
	
	[Event(name="add_note", type="net.yambal.RTM.Event.NoteEvent")]
	[Event(name="edit_note", type="net.yambal.RTM.Event.NoteEvent")]
	[Event(name="delete_note", type="net.yambal.RTM.Event.NoteEvent")]
	public class Notes extends EventDispatcher
	{
		
		//　-----------------------------------------------------------------------------------
		/**
		 * ノートを追加
		 **/
		public function add(api_key:String, shared_secret:String, token:String, task_series:RTMTaskSeries, note_title:String, note_text:String):void{
			var RM:RestManager = new RestManager();
			
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = token;
			p.list_id = task_series.list_id;
			p.note_text = note_title;
			p.note_title = note_text;
			p.task_id = task_series.task.id;
			p.taskseries_id = task_series.id;
			
			RM.doMethod(RestService.METHOD_NOTES_ADD, p, this.onAdd, this.onFaultAdd, true, shared_secret, "ノート追加");
			
		}
		
		private function onFaultAdd(code:Number, massage:String, ro:Object):void{
			// エラー処理
		}
		
		private function onAdd(resultXML:XML, ro:Object, t:RTMTransaction):void{
			var resNoteArray:Array = Parser.NoteXMLListToNote(resultXML.descendants("note"));
			this.dispatchEvent(new NoteEvent(NoteEvent.ADD, false, false, resNoteArray, t));
		}
		
		//　-----------------------------------------------------------------------------------
		/**
		 * ノートを編集
		 **/
		public function edit(api_key:String, shared_secret:String, token:String, note_id:uint, note_title:String, note_text:String):void{
			var RM:RestManager = new RestManager();
			
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = token;
			p.note_id = note_id;
			p.note_title = note_title;
			p.note_text = note_text;
			
			RM.doMethod(RestService.METHOD_NOTES_EDIT, p, this.onEdit, this.onFaultEdit, true, shared_secret, "ノート編集");
		}
		
		private function onFaultEdit(code:Number, massage:String, ro:Object):void{
			// エラー処理
		}
		
		private function onEdit(result_xml:XML, ro:Object, t:RTMTransaction):void{
			var resNoteArray:Array = Parser.NoteXMLListToNote(result_xml.descendants("note"));
			this.dispatchEvent(new NoteEvent(NoteEvent.EDIT, false, false, resNoteArray, t));
		}
		
		//　-----------------------------------------------------------------------------------
		/**
		 * ノートを削除
		 **/
		public function deleteNote(api_key:String, shared_secret:String, auth_token:String, note_id:uint):void{
			var RM:RestManager = new RestManager();
			
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.note_id = note_id;
			
			RM.doMethod(RestService.METHOD_NOTES_DELETE, p, this.onDelete, this.onFaultDelete, true, shared_secret);
		}
		
		private function onFaultDelete(code:Number, massage:String, ro:Object):void{
			// エラー処理
		}
		
		public function onDelete(resultXML:XML, ro:Object, t:RTMTransaction):void{
			this.dispatchEvent(new NoteEvent(NoteEvent.DELETE, false, false, null, t));
		}
	}
}